package com.facebook.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import com.facebook.FacebookException;
import com.facebook.internal.Utility;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

class ImageDownloader
{
  private static final int CACHE_READ_QUEUE_MAX_CONCURRENT = 2;
  private static final int DOWNLOAD_QUEUE_MAX_CONCURRENT = 8;
  private static WorkQueue cacheReadQueue = new WorkQueue(2);
  private static WorkQueue downloadQueue;
  private static final Handler handler = new Handler();
  private static final Map<RequestKey, DownloaderContext> pendingRequests = new HashMap();

  static
  {
    downloadQueue = new WorkQueue(8);
  }

  static boolean cancelRequest(ImageRequest paramImageRequest)
  {
    RequestKey localRequestKey = new RequestKey(paramImageRequest.getImageUrl(), paramImageRequest.getCallerTag());
    synchronized (pendingRequests)
    {
      DownloaderContext localDownloaderContext = (DownloaderContext)pendingRequests.get(localRequestKey);
      boolean bool = false;
      if (localDownloaderContext != null)
      {
        bool = true;
        if (localDownloaderContext.workItem.cancel())
          pendingRequests.remove(localRequestKey);
      }
      else
      {
        return bool;
      }
      localDownloaderContext.isCancelled = true;
    }
  }

  private static void download(RequestKey paramRequestKey, Context paramContext)
  {
    HttpURLConnection localHttpURLConnection = null;
    InputStream localInputStream = null;
    Bitmap localBitmap = null;
    int i = 1;
    try
    {
      localHttpURLConnection = (HttpURLConnection)paramRequestKey.url.openConnection();
      localHttpURLConnection.setInstanceFollowRedirects(false);
      switch (localHttpURLConnection.getResponseCode())
      {
      default:
        localInputStream = localHttpURLConnection.getErrorStream();
        localInputStreamReader = new InputStreamReader(localInputStream);
        char[] arrayOfChar = new char[''];
        localStringBuilder = new StringBuilder();
        while (true)
        {
          int j = localInputStreamReader.read(arrayOfChar, 0, arrayOfChar.length);
          if (j <= 0)
            break;
          localStringBuilder.append(arrayOfChar, 0, j);
        }
      case 301:
      case 302:
      case 200:
      }
    }
    catch (IOException localIOException)
    {
      InputStreamReader localInputStreamReader;
      StringBuilder localStringBuilder;
      Object localObject2 = localIOException;
      Utility.closeQuietly(localInputStream);
      Utility.disconnectQuietly(localHttpURLConnection);
      if (i != 0)
        issueResponse(paramRequestKey, (Exception)localObject2, localBitmap, false);
      return;
      i = 0;
      String str = localHttpURLConnection.getHeaderField("location");
      boolean bool1 = Utility.isNullOrEmpty(str);
      localBitmap = null;
      localObject2 = null;
      i = 0;
      localInputStream = null;
      if (!bool1)
      {
        URL localURL = new URL(str);
        UrlRedirectCache.cacheUrlRedirect(paramContext, paramRequestKey.url, localURL);
        DownloaderContext localDownloaderContext = removePendingRequest(paramRequestKey);
        localBitmap = null;
        localObject2 = null;
        i = 0;
        localInputStream = null;
        if (localDownloaderContext != null)
        {
          boolean bool2 = localDownloaderContext.isCancelled;
          localBitmap = null;
          localObject2 = null;
          i = 0;
          localInputStream = null;
          if (!bool2)
            enqueueCacheRead(localDownloaderContext.request, new RequestKey(localURL, paramRequestKey.tag), false);
        }
      }
      while (true)
      {
        Utility.closeQuietly(localInputStream);
        Utility.disconnectQuietly(localHttpURLConnection);
        break;
        localInputStream = ImageResponseCache.interceptAndCacheImageStream(paramContext, localHttpURLConnection);
        localBitmap = BitmapFactory.decodeStream(localInputStream);
        localObject2 = null;
        continue;
        Utility.closeQuietly(localInputStreamReader);
        localObject2 = new FacebookException(localStringBuilder.toString());
        localBitmap = null;
      }
    }
    finally
    {
      Utility.closeQuietly(localInputStream);
      Utility.disconnectQuietly(localHttpURLConnection);
    }
  }

  static void downloadAsync(ImageRequest paramImageRequest)
  {
    if (paramImageRequest == null)
      return;
    RequestKey localRequestKey = new RequestKey(paramImageRequest.getImageUrl(), paramImageRequest.getCallerTag());
    while (true)
    {
      synchronized (pendingRequests)
      {
        DownloaderContext localDownloaderContext = (DownloaderContext)pendingRequests.get(localRequestKey);
        if (localDownloaderContext != null)
        {
          localDownloaderContext.request = paramImageRequest;
          localDownloaderContext.isCancelled = false;
          localDownloaderContext.workItem.moveToFront();
          return;
        }
      }
      enqueueCacheRead(paramImageRequest, localRequestKey, paramImageRequest.isCachedRedirectAllowed());
    }
  }

  private static void enqueueCacheRead(ImageRequest paramImageRequest, RequestKey paramRequestKey, boolean paramBoolean)
  {
    enqueueRequest(paramImageRequest, paramRequestKey, cacheReadQueue, new CacheReadWorkItem(paramImageRequest.getContext(), paramRequestKey, paramBoolean));
  }

  private static void enqueueDownload(ImageRequest paramImageRequest, RequestKey paramRequestKey)
  {
    enqueueRequest(paramImageRequest, paramRequestKey, downloadQueue, new DownloadImageWorkItem(paramImageRequest.getContext(), paramRequestKey));
  }

  private static void enqueueRequest(ImageRequest paramImageRequest, RequestKey paramRequestKey, WorkQueue paramWorkQueue, Runnable paramRunnable)
  {
    synchronized (pendingRequests)
    {
      DownloaderContext localDownloaderContext = new DownloaderContext(null);
      localDownloaderContext.request = paramImageRequest;
      pendingRequests.put(paramRequestKey, localDownloaderContext);
      localDownloaderContext.workItem = paramWorkQueue.addActiveWorkItem(paramRunnable);
      return;
    }
  }

  private static void issueResponse(RequestKey paramRequestKey, final Exception paramException, final Bitmap paramBitmap, final boolean paramBoolean)
  {
    DownloaderContext localDownloaderContext = removePendingRequest(paramRequestKey);
    if ((localDownloaderContext != null) && (!localDownloaderContext.isCancelled))
    {
      ImageRequest localImageRequest = localDownloaderContext.request;
      final ImageRequest.Callback localCallback = localImageRequest.getCallback();
      if (localCallback != null)
        handler.post(new Runnable()
        {
          public void run()
          {
            ImageResponse localImageResponse = new ImageResponse(this.val$request, paramException, paramBoolean, paramBitmap);
            localCallback.onCompleted(localImageResponse);
          }
        });
    }
  }

  static void prioritizeRequest(ImageRequest paramImageRequest)
  {
    RequestKey localRequestKey = new RequestKey(paramImageRequest.getImageUrl(), paramImageRequest.getCallerTag());
    synchronized (pendingRequests)
    {
      DownloaderContext localDownloaderContext = (DownloaderContext)pendingRequests.get(localRequestKey);
      if (localDownloaderContext != null)
        localDownloaderContext.workItem.moveToFront();
      return;
    }
  }

  private static void readFromCache(RequestKey paramRequestKey, Context paramContext, boolean paramBoolean)
  {
    InputStream localInputStream = null;
    boolean bool = false;
    if (paramBoolean)
    {
      URL localURL = UrlRedirectCache.getRedirectedUrl(paramContext, paramRequestKey.url);
      localInputStream = null;
      bool = false;
      if (localURL != null)
      {
        localInputStream = ImageResponseCache.getCachedImageStream(localURL, paramContext);
        if (localInputStream == null)
          break label81;
        bool = true;
      }
    }
    if (!bool)
      localInputStream = ImageResponseCache.getCachedImageStream(paramRequestKey.url, paramContext);
    if (localInputStream != null)
    {
      Bitmap localBitmap = BitmapFactory.decodeStream(localInputStream);
      Utility.closeQuietly(localInputStream);
      issueResponse(paramRequestKey, null, localBitmap, bool);
    }
    label81: DownloaderContext localDownloaderContext;
    do
    {
      return;
      bool = false;
      break;
      localDownloaderContext = removePendingRequest(paramRequestKey);
    }
    while ((localDownloaderContext == null) || (localDownloaderContext.isCancelled));
    enqueueDownload(localDownloaderContext.request, paramRequestKey);
  }

  private static DownloaderContext removePendingRequest(RequestKey paramRequestKey)
  {
    synchronized (pendingRequests)
    {
      DownloaderContext localDownloaderContext = (DownloaderContext)pendingRequests.remove(paramRequestKey);
      return localDownloaderContext;
    }
  }

  private static class CacheReadWorkItem
    implements Runnable
  {
    private boolean allowCachedRedirects;
    private Context context;
    private ImageDownloader.RequestKey key;

    CacheReadWorkItem(Context paramContext, ImageDownloader.RequestKey paramRequestKey, boolean paramBoolean)
    {
      this.context = paramContext;
      this.key = paramRequestKey;
      this.allowCachedRedirects = paramBoolean;
    }

    public void run()
    {
      ImageDownloader.readFromCache(this.key, this.context, this.allowCachedRedirects);
    }
  }

  private static class DownloadImageWorkItem
    implements Runnable
  {
    private Context context;
    private ImageDownloader.RequestKey key;

    DownloadImageWorkItem(Context paramContext, ImageDownloader.RequestKey paramRequestKey)
    {
      this.context = paramContext;
      this.key = paramRequestKey;
    }

    public void run()
    {
      ImageDownloader.download(this.key, this.context);
    }
  }

  private static class DownloaderContext
  {
    boolean isCancelled;
    ImageRequest request;
    WorkQueue.WorkItem workItem;
  }

  private static class RequestKey
  {
    private static final int HASH_MULTIPLIER = 37;
    private static final int HASH_SEED = 29;
    Object tag;
    URL url;

    RequestKey(URL paramURL, Object paramObject)
    {
      this.url = paramURL;
      this.tag = paramObject;
    }

    public boolean equals(Object paramObject)
    {
      boolean bool1 = false;
      if (paramObject != null)
      {
        boolean bool2 = paramObject instanceof RequestKey;
        bool1 = false;
        if (bool2)
        {
          RequestKey localRequestKey = (RequestKey)paramObject;
          if ((localRequestKey.url != this.url) || (localRequestKey.tag != this.tag))
            break label51;
          bool1 = true;
        }
      }
      return bool1;
      label51: return false;
    }

    public int hashCode()
    {
      return 37 * (1073 + this.url.hashCode()) + this.tag.hashCode();
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.facebook.widget.ImageDownloader
 * JD-Core Version:    0.6.2
 */